home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Atari Mega Archive 1
/
Atari Mega Archive - Volume 1.iso
/
mint
/
toswinsc.zoo
/
util.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-27
|
20KB
|
892 lines
/*
* Copyright 1992 Eric R. Smith. All rights reserved.
* Redistribution is permitted only if the distribution
* is not for profit, and only if all documentation
* (including, in particular, the file "copying")
* is included in the distribution in unmodified form.
* THIS PROGRAM COMES WITH ABSOLUTELY NO WARRANTY, NOT
* EVEN THE IMPLIED WARRANTIES OF MERCHANTIBILITY OR
* FITNESS FOR A PARTICULAR PURPOSE. USE AT YOUR OWN
* RISK.
*/
#include "xgem.h"
#include <string.h>
#include <stdlib.h>
#include "toswin.h"
#include "twdefs.h"
#include "twproto.h"
extern char *read_scrap();
extern void write_scrap();
/* shift key states */
#define KLSHIFT 0x1
#define KRSHIFT 0x2
#define KCTRL 0x4
#define KALT 0x8
#define KANY (0xf)
/* what to add at the end of lines */
#define CR 1
#define LF 2
#define CRLF 3
#ifdef NONE
#undef NONE
#endif
#define NONE 0
int paste_options = CR;
/* how to cut lines */
#define STRIPBLANKS 1
#define OBEYLINES 2
int cut_options = STRIPBLANKS;
void
setcutoptions()
{
int xclip, yclip, wclip, hclip;
int ret;
cutdialog[ADDCRLF].ob_state =
cutdialog[ADDLF].ob_state =
cutdialog[ADDCR].ob_state =
cutdialog[ADDNONE].ob_state = NORMAL;
switch(paste_options) {
case CR:
ret = ADDCR; break;
case LF:
ret = ADDLF; break;
case CRLF:
ret = ADDCRLF; break;
default:
ret = NONE; break;
}
cutdialog[ret].ob_state = SELECTED;
cutdialog[STRIPEOL].ob_state = (cut_options & STRIPBLANKS) ? SELECTED :
NORMAL;
cutdialog[OBEYEOL].ob_state = (cut_options & OBEYLINES) ? SELECTED :
NORMAL;
wind_update(1);
form_center(cutdialog, &xclip, &yclip, &wclip, &hclip);
form_dial(FMD_START, 0, 0, 32, 32, xclip, yclip, wclip, hclip);
if (win_flourishes)
form_dial(FMD_GROW, 0, 0, 32, 32, xclip, yclip, wclip, hclip);
objc_draw(cutdialog, 0, 2, xclip, yclip, wclip, hclip);
ret = form_do(cutdialog, 0);
if (win_flourishes)
form_dial(FMD_SHRINK, 0, 0, 32, 32, xclip, yclip, wclip, hclip);
form_dial(FMD_FINISH, 0, 0, 32, 32, xclip, yclip, wclip, hclip);
wind_update(0);
cutdialog[ret].ob_state = NORMAL;
if (ret == EDOK) {
cut_options = NONE;
if (cutdialog[STRIPEOL].ob_state == SELECTED)
cut_options |= STRIPBLANKS;
if (cutdialog[OBEYEOL].ob_state == SELECTED)
cut_options |= OBEYLINES;
if (cutdialog[ADDCR].ob_state == SELECTED)
paste_options = CR;
else if (cutdialog[ADDLF].ob_state == SELECTED)
paste_options = LF;
else if (cutdialog[ADDCRLF].ob_state == SELECTED)
paste_options = CRLF;
else
paste_options = NONE;
}
}
/* unselect all text in the indicated window */
void
unselect(t)
TEXTWIN *t;
{
int i, j;
for (i = 0; i < t->maxy; i++)
for (j = 0; j < t->maxx; j++) {
if (t->cflag[i][j] & CSELECTED) {
t->cflag[i][j] &= ~CSELECTED;
t->cflag[i][j] |= CTOUCHED;
t->dirty[i] |= SOMEDIRTY;
}
}
refresh_textwin(t);
}
/* cut selected text from a window */
char *cliptext = 0;
void
cut(w)
WINDOW *w;
{
TEXTWIN *t;
int i, j, numchars, numlines;
int linedone;
int needcrlf;
char *s, c;
if (w->wtype != TEXT_WIN) return;
t = w->extra;
if (cliptext) {
free(cliptext);
cliptext = 0;
}
numchars = numlines = 0;
for (i = 0; i < t->maxy; i++) {
linedone = 0;
for (j = 0; j < t->maxx; j++) {
if (t->cflag[i][j] & CSELECTED) {
numchars++;
if (!linedone) {
numlines++;
linedone = 1;
}
}
}
}
if (!numchars) {
form_alert(1, AlertStrng(NOTEXT));
return;
}
/*
* The cut strategy is a little complicated, but here it is:
* A "line" is a continuous stream of characters. If the "Obey Lines"
* option is set, lines always end at end-of-line; otherwise, they
* end at eol only if there are 2 or more spaces after them.
* If "Strip Blanks" is set, then any trailing blanks are deleted.
*/
cliptext = malloc(numchars+numlines+numlines+1);
if (!cliptext) {
form_alert(1, AlertStrng(NOMEM));
return;
}
s = cliptext;
needcrlf = 0;
for (i = 0; i < t->maxy; i++) {
linedone = 0;
for (j = 0; j < t->maxx; j++) {
if (t->cflag[i][j] & CSELECTED) {
c = t->data[i][j];
if (!c) c = ' ';
*s++ = c;
needcrlf = 1;
if ( (cut_options & OBEYLINES) ||
(j < t->maxy-2) ||
((c == ' ') && t->data[i][j-1] == ' '))
linedone = 1;
else
linedone = 0;
}
}
if (linedone) {
if (cut_options & STRIPBLANKS) {
while (s > cliptext && s[-1] == ' ')
--s;
}
*s++ = '\r'; *s++ = '\n';
needcrlf = 0;
}
}
/* tie off cliptext */
if (needcrlf) {
*s++ = '\r'; *s++ = '\n';
}
*s++ = 0;
unselect(t);
}
/* paste text into a window */
void
paste(w)
WINDOW *w;
{
char *s;
int c;
if (!cliptext) {
form_alert(1, AlertStrng(NOCUT));
return;
}
for (s = cliptext; *s; s++) {
c = *(unsigned char *)s;
if (c == '\r' && s[1] == '\n') {
s++;
switch(paste_options) {
case CRLF:
(*w->keyinp)(w, '\r', 0);
case LF:
(*w->keyinp)(w, '\n', 0);
break;
case CR:
(*w->keyinp)(w, '\r', 0);
break;
case NONE:
break;
}
} else
(*w->keyinp)(w, c, 0);
}
}
void
redraw_screen(x, y, w, h)
int x, y, w, h;
{
form_dial(0, x, y, w, h, x, y, w, h);
form_dial(3, x, y, w, h, x, y, w, h);
}
/* cut text from the desktop */
void
cut_from_desk(x, y)
int x, y;
{
int i;
int width, height, x1, y1, x2, y2, dummy;
WINDOW *w;
i = objc_find(deskobj, 0, 1, x, y);
if (i != CLIPICN) {
if (gl_topwin && gl_topwin->wtype == TEXT_WIN)
unselect(gl_topwin->extra);
return;
}
width = deskobj[i].ob_width; height = deskobj[i].ob_height;
objc_offset(deskobj, i, &x1, &y1);
/* drag the icon around */
wind_update(BEG_MCTRL);
graf_mouse(FLAT_HAND, 0L);
graf_dragbox(width, height, x1, y1, xdesk, ydesk, wdesk, hdesk,
&x2, &y2);
graf_mkstate(&x, &y, &dummy, &dummy);
graf_mouse(ARROW, 0L);
wind_update(END_MCTRL);
/* did we actually move anywhere? */
if (x >= x1 && x <= x1+width && y >= y1 && y <= y1+height) {
return;
}
w = find_window(x, y);
if (w && w->wtype == TEXT_WIN) {
if (cliptext)
free(cliptext);
cliptext = read_scrap("SCRAP.TXT");
if (!cliptext) {
form_alert(1, AlertStrng(SCRAPDAT));
return;
}
paste(w);
#if 0
} else { /* just move the icon */
hide_mouse();
deskobj[i].ob_x += x2 - x1;
deskobj[i].ob_y += y2 - y1;
redraw_screen(x1, y1, width, height);
redraw_screen(x2, y2, width, height);
show_mouse();
#endif
}
}
/* paste text onto the desktop */
void
paste_to_desk(x, y)
int x, y;
{
int i;
int x1, y1;
WINDOW *w;
w = find_window(x, y);
i = objc_find(deskobj, 0, 1, x, y);
if (i == CLIPICN) {
objc_offset(deskobj, CLIPICN, &x1, &y1);
objc_change(deskobj, CLIPICN, 0, xdesk, ydesk, wdesk, hdesk,
SELECTED, 0);
if (w == toolwindow) {
redraw_window(toolwindow, x1, y1,
deskobj[i].ob_width, deskobj[i].ob_height);
}
write_scrap("SCRAP.TXT", cliptext, (int)strlen(cliptext));
objc_change(deskobj, CLIPICN, 0, xdesk, ydesk, wdesk, hdesk,
NORMAL, 0);
if (w == toolwindow) {
redraw_window(toolwindow, x1, y1,
deskobj[i].ob_width, deskobj[i].ob_height);
}
}
}
static void
lightbox(plin, numpoints)
int *plin;
int numpoints;
{
static int vtbl[4] = { 0x5555, 0xaaaa, 0xaaaa, 0x5555 };
static int htbl[2] = { 0x5555, 0xaaaa };
int style, *linexy, i;
int attrib[6];
vql_attribute(vdi_handle, attrib);
vsl_color(vdi_handle, 1);
for (i = 1; i < numpoints; i++) {
if (plin[0] == plin[2])
style = vtbl[(plin[0] & 1) | ((plin[1] & 1) << 1)];
else {
linexy = (plin[0] < plin[2]) ? plin : plin+2;
style = htbl[ linexy[1] & 1 ];
}
vsl_udsty(vdi_handle, style);
vsl_type(vdi_handle, 7);
v_pline(vdi_handle, 2, plin);
plin += 2;
}
vsl_type(vdi_handle, attrib[0]);
vsl_color(vdi_handle, attrib[1]);
}
void
hot_dragbox(plin, numpoints, lastx, lasty)
int *plin;
int numpoints;
int *lastx, *last